<include file="Application/Home/View/Public/header.html" title="教师注册"/>
<div class="container">
    <!-- 错误信息提示框-->
    <div class="alert alert-danger hidden" role="alert"></div>
    <div class="page-header">
        <h1>教师注册</h1>
    </div>
    <!--<form class="col-md-4" action="__URL__/login" method="post">-->
    <form class="col-md-4" id="tea-register-form">
        <div class="form-group has-feedback" id="email">
            <label for="InputEmail1">登录号：</label>
            <input type="email" class="form-control" placeholder="请输入邮箱"
                   required>
            <span class="help-block"></span>
            <span class="glyphicon form-control-feedback hidden"></span>
        </div>
        <div class="form-group" id="choose_college">
            <label for="InputSchool">学校：</label>
            <select class="form-control">
                <foreach name="college" item="vo">
                    <option value="{$vo.col_id}">{$vo.col_name}</option>
                </foreach>
            </select>
        </div>
        <div class="form-group has-feedback" id="teacher_id">
            <label class="form-group-label" for="InputTeacherNumber">教师证号：</label>
            <input type="text" class="form-control" placeholder="请输入您的教师证号" 
                   required pattern="^[a-zA-Z0-9]+$">
            <span class="help-block"></span>
            <span class="glyphicon form-control-feedback hidden"></span>
        </div>
        <div class="form-group" id="name">
            <label for="InputName">姓名：</label>
            <input type="text" class="form-control" 
                   placeholder="请输入您的真实姓名" required>
        </div>
        <div class="form-group has-feedback" id="password">
            <label for="exampleInputPassword1">密码：</label>
            <input type="password" class="form-control" 
                   placeholder="请输入您的密码" required>
            <span class="help-block"></span>
            <span class="glyphicon form-control-feedback hidden"></span>
        </div>
        <div class="form-group has-feedback" id="password2">
            <label for="exampleInputPassword2">确认密码：</label>
            <input type="password" class="form-control" 
                   placeholder="确认密码" required>
            <span class="help-block"></span>
            <span class="glyphicon form-control-feedback hidden"></span>
        </div>
        <input type="submit" id="register" class="btn btn-default" value="注册" alt="注册"/>
        <input type="hidden" id="url" value="__MODULE__"/>
    </form>
</div>

<script type="text/javascript">
    $(function(){
        // 获取对象
        var input_email = $("#email > input");
        var input_choose_college = $("#choose_college > select");
        var input_teacher_id = $("#teacher_id > input");
        var input_name = $("#name > input");
        var input_pwd = $("#password > input");
        var input_pwd2 = $("#password2 > input");

        var div_email = $("#email");
        var div_choose_college = $("#choose_college");
        var div_teacher_id = $("#teacher_id");
        var div_name = $("#name");
        var div_pwd = $("#password");
        var div_pwd2 = $("#password2");
        var submitBtn = $("#register");

        // 监听email变化
        input_email.change(function() {
            var email =input_email.val();
            if (/^[a-z0-9\.\-\_]+\@[a-z0-9\-\_]+(\.[a-z0-9\-\_]+){1,4}$/.test(email) === false) {
                addError(div_email, "请输入正确的邮箱格式");
                return;
            }
            postApi("__MODULE__/Auth/check_email", {email:email}, function(err, data) {
                if (err) {
                    addError(div_email,"邮箱已存在");
                    input_email.focus();
                } else {
                    addSuccess(div_email);
                }
            });
        });

        handle_change = function() {
            showError("");
            var col_id = input_choose_college.val();    // 学校id号
            var tea_number = input_teacher_id.val();
            // 教职证号不为空时才提交
            if (tea_number !== "") {
                postApi("__MODULE__/Auth/check_tea_number", {tea_number:tea_number,col_id:col_id}, 
                    function(err, data) {
                    if (err) {
                        addError(div_teacher_id, "当前学校下教职证号已存在");
                        input_teacher_id.focus();
                    } else {
                        addSuccess(div_teacher_id);
                    }
                });
            }
        };

        // 用同一个函数监听学校选择框和老师id填写框
        input_choose_college.change(handle_change);
        input_teacher_id.change(handle_change);

        // 监听两个密码输入框
        input_pwd2.change(function() {
            var pwd = input_pwd.val();
            var pwd2 = input_pwd2.val();
            // 两次输入不同时，清空确认的输入框，并将焦点定位到第一个输入框
            if (pwd !== pwd2) {
                addError(div_pwd, "两次输入的密码不同");
                addError(div_pwd2, "");
                input_pwd.focus();
                input_pwd2.val("");
            } else {
                addSuccess(div_pwd);
                addSuccess(div_pwd2);
            }
        });

        // 提交表单
        $("#tea-register-form").submit(function(e){
            e.preventDefault(); // 阻止表单的提交
            $(this).blur();
            var email = input_email.val();
            var choose_college = input_choose_college.val();
            var teacher_number = input_teacher_id.val();
            var name = input_name.val();
            var pwd = input_pwd.val();
            var pwd2 = input_pwd2.val();

            if (email === "") {
                showError("登录号不能为空");
                return;
            }
            if (choose_college === "") {
                showError("请选择一个学校");
                return;
            }
            if (teacher_number === "") {
                showError("教职工号不能为空");
                return;
            }
            if (pwd === "" || pwd2 === "") {
                showError("密码不能空");
                return;
            }

            postData = {email:email,col_id:choose_college,
                    tea_number:teacher_number,name:name,password:CryptoJS.MD5(pwd).toString()};
            postApi("__MODULE__/Auth/tea_register", postData, function(err, data) {
                if (err) {
                    showError(err.message);
                } else {
                    //所有验证都已经通过,跳转到主页面
                    window.location.href = $("#url").val()+"/Auth/login";
                }
            });
        });
    });
</script>